﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ExcelOperateForNPOI;
using Time;
using System.IO;
using System.Threading;

namespace ExpTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            this.dataTable1.Clear();
            InitData();
            label2.Text = this.dataTable1.Rows.Count.ToString();
        }

        private void InitData()
        {
            for (int i = 0; i < 10000; i++)
            {
                DataRow iDataRow = this.dataTable1.NewRow();
                iDataRow[0] = "学号" + i.ToString();
                iDataRow[1] = "姓名" + i.ToString();
                iDataRow[2] = "人员类型" + i.ToString();
                iDataRow[3] = "职务" + i.ToString();
                iDataRow[4] = "性别" + i.ToString();
                iDataRow[5] = "备注" + i.ToString();
                iDataRow[6] = "住宿情况" + i.ToString();
                iDataRow[7] = "部门名称" + i.ToString();

                this.dataTable1.Rows.Add(iDataRow);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Timeing.LongFrequency();
            Timeing.Timeing_Begin();

            C_ExpInfo iMyExpInfo = new C_ExpInfo();

            for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
            {
                iMyExpInfo.ClumnName.Add(this.dataGridView1.Columns[i].DataPropertyName
                    , this.dataGridView1.Columns[i].HeaderText);
            }

            DataRow iMyExp = iMyExpInfo.ExpData.Tables["ExpSheets"].Rows.Add();
            iMyExp["ExpID"] = 1;
            iMyExp["ExpDataName"] = this.dataTable1.TableName;
            iMyExp["ExpSheetName"] = "人员信息";
            iMyExp["IfUserSele"] = false;

            DataRow iMyExpInfoRow = iMyExpInfo.ExpData.Tables["AllInfo"].Rows.Add();
            iMyExpInfoRow["ID"] = 1;
            iMyExpInfoRow["Text"] = "人员信息";
            iMyExpInfoRow["IfRag"] = true;
            iMyExpInfoRow["IfAutoCol"] = true;
            iMyExpInfoRow["ColNum_B"] = 0;
            iMyExpInfoRow["RowNum_B"] = 0;
            iMyExpInfoRow["ColSum"] = 8;
            iMyExpInfoRow["RowSum"] = 2;
            iMyExpInfoRow["Fonts"] = 20;

            iMyExpInfo.ExpData.Tables.Add(this.dataTable1.Copy());

            if (C_ExcelForNPOI.ExportExcelFile(iMyExpInfo, "d:\\123Npoi.xls") == false)
            {
                MessageBox.Show(C_ExcelForNPOI.ErrMeg);
            }

            Timeing.Timeing_End();
            Timeing.Time_Count();
            label1.Text = Timeing.TimeCount.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("d:\\12345.xlsx") == false)
            {
                return;
            }

            using (Stream iFile = File.Open("d:\\12345.xlsx", FileMode.Open, FileAccess.Read))
            {
                bool iRet = C_ExcelForNPOI.CheckIsExcel03File(iFile);
            }
        }

        private void btn_dc_Click(object sender, EventArgs e)
        {
            if(dataTable1.Rows.Count<=0)
            {
                MessageBox.Show("没有需要导出的数据！");
                return;
            }

            string iModeFilePath = Application.StartupPath + @"/mouldFile.xlsx";
            if (!File.Exists(iModeFilePath))
            {
                MessageBox.Show("没有找到指定的模板文件。", "提示");
                return;
            }

            List<C_DataMod> iDatas = new List<C_DataMod>();
            foreach(DataRow iDataRowObj in dataTable1.Rows)
            {
                C_DataMod iData = new C_DataMod();
                iData.xh = iDataRowObj["xh"].ToString();
                iData.xm = iDataRowObj["xm"].ToString();
                iData.rylx = iDataRowObj["rylx"].ToString();
                iData.xb = iDataRowObj["xb"].ToString();
                iData.zsqk = iDataRowObj["zsqk"].ToString();
                iData.zw = iDataRowObj["zw"].ToString();
                iData.bmmc = iDataRowObj["bmmc"].ToString();
                iData.bz = iDataRowObj["bz"].ToString();

                iDatas.Add(iData);
            }
            List<C_SumDataMod> iSumDatas = new List<C_SumDataMod>();
            C_SumDataMod iSumData = new C_SumDataMod();
            iSumData.hj = dataTable1.Rows.Count;
            iSumDatas.Add(iSumData);

            C_ExcelForNPOI iExcelObj = new C_ExcelForNPOI();
            iExcelObj.ShowExportState += IExcelObj_ShowExportState;
            iExcelObj.LoadMouldFile(iModeFilePath);
            iExcelObj.RegisterData(iDatas, "Datas", true);
            iExcelObj.RegisterData(iSumDatas, "SumDatas");
            if (aFrm_Speed == null) aFrm_Speed = new Frm_Speed();
            aFrm_Speed.SetMaximum(iDatas.Count);
            aFrm_Speed.Show();
            Thread iNewThread = new Thread(new ThreadStart(iExcelObj.ExportForMould));
            iNewThread.Start();
        }

        private delegate void ShowExportState(int count);
        private Frm_Speed aFrm_Speed = null;

        private void IExcelObj_ShowExportState(ExportState state, int count)
        {
            switch (state)
            {
                case ExportState.READY:
                    {
                        aFrm_Speed.SetExportState(count, "就绪，准备导出……");
                        break;
                    }
                case ExportState.DOING:
                    {
                        aFrm_Speed.SetExportState(count, string.Format("正在导出第 {0} 条数据……",count));
                        break;
                    }
                case ExportState.DONE:
                    {
                        aFrm_Speed.SetExportState(0, "导出完成。");
                        aFrm_Speed.CloseFrom();
                        break;
                    }
            }
        }

        private void SetExportState(int count)
        {
            if (progressBar1.InvokeRequired)
            {
                ShowExportState iSetExportState = new ShowExportState(SetExportState);
                progressBar1.Invoke(iSetExportState, new object[] { count });
            }
            else
            {
                if (count <= progressBar1.Maximum)
                {
                    progressBar1.Value = count;
                }
            }
        }
    }
}